package com.ems.controller;

import com.ems.entity.TOrder;
import com.ems.service.ITOrderService;
import com.ems.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZH
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/t-order")
public class TOrderController {
    @Autowired
    private ITOrderService itOrderService;

//订单增删改查部分
    @GetMapping("/list")
    public ResultUtil list(){
        if (itOrderService.list().size()>0){
            return ResultUtil.success(itOrderService.list());
        }else {
            return ResultUtil.fail("查询失败");
        }
    }
    //订单号查询
    @GetMapping("/getByOrderNumber")
    public ResultUtil getByOrderId(@RequestParam Integer orderNumber){
        if (itOrderService.getByOrderNumber(orderNumber)!=null){
            return ResultUtil.success(itOrderService.getByOrderNumber(orderNumber));
        }else {
            return ResultUtil.fail("查询失败");
        }
    }
    //    逻辑删除
    @DeleteMapping("/removeByLogic/{idList}")
    public ResultUtil removeByLogic(@PathVariable("idList") Collection<Integer> idList){
        if (itOrderService.removeByIds(idList)){
            return ResultUtil.success("删除成功"+idList.size()+"条");
        }else {
            return ResultUtil.fail("删除失败");
        }
    }
    //新增
    @PostMapping("/add")
    public ResultUtil add(@RequestBody TOrder tOrder){
        if (itOrderService.save(tOrder)){
            return ResultUtil.success("添加成功");
        }else {
            return ResultUtil.fail("添加失败");
        }
    }
    //修改
    @PutMapping("/update")
    public ResultUtil update(@RequestBody TOrder tOrder){
        if (itOrderService.updateById(tOrder)){
            return ResultUtil.success("修改成功");
        }else {
            return ResultUtil.fail("修改失败");
        }
    }

    //业务部分
    //生产派工
    @PutMapping("/dispatch")
    public ResultUtil dispatch(@RequestBody TOrder tOrder){
        if (itOrderService.dispatch(tOrder)){
            return ResultUtil.success("派工成功");
        }else {
            return ResultUtil.fail("派工失败");
        }

    }
    //确认执行
    @PutMapping("/confirm")
    public ResultUtil confirm(@RequestBody TOrder tOrder){
        if (itOrderService.confirm(tOrder)){
            return ResultUtil.success("确认执行成功");
        }else {
            return ResultUtil.fail("确认执行失败");
        }

    }
    //查询生产通知
    @GetMapping("/listDispatch")
    public ResultUtil listDispatch(){
        if (itOrderService.listDispatch().size()>0){
            return ResultUtil.success(itOrderService.listDispatch());
        }else {
            return ResultUtil.fail("查询失败");
        }
    }
    //订单完成
    @PutMapping("/finish")
    public ResultUtil finish(@RequestBody TOrder tOrder){
        if (itOrderService.finish(tOrder)){
            return ResultUtil.success("订单完成成功");
        }else {
            return ResultUtil.fail("订单完成失败");
        }
    }

    //工单部分
    //工单详情
    @GetMapping("/detail")
    public ResultUtil detail(){
        if (itOrderService.detail().size()>0){
            return ResultUtil.success(itOrderService.detail());
        }else {
            return ResultUtil.fail("查询失败");
        }

    }
}
